﻿Chenxin.UI.CommissionScale = Chenxin.extend(Chenxin.UI.PageCommon, {
    autoInitForm: false,
    actionUrl: '/service/logic/CommissionScale.ashx',
    createSearch: function() {
        return new Chenxin.UI.CommissionScaleSearch({
            scope: this
        });
    },
    createGrid: function() {
        return new Chenxin.UI.JsonGrid({
            actionUrl: this.actionUrl + '?op=getlist',
            pageSize: 100,
            hasPage: true,
            cellBorder: true,
            height: 400,
            autoLoad: false,
            renderTo: 'CommissionScale_table',
            hasCheckBox: true,
            id: 'searchtable',
            modelId: 'RecordId',
            title: '佣金比例设定',
            scope: this,
            columns: {
                Company: {
                    width: 120,
                    head: '公司名称',
                    sortable: true
                },
                AchievementsMin: {
                    width: 120,
                    head: '最小金额',
                    sortable: true
                },
                AchievementsMax: {
                    width: 140,
                    head: '最大金额',
                    sortable: true
                },
                Ratio: {
                    width: 120,
                    head: '佣金比例',
                    sortable: true
                },
                Deduct: {
                    width: 120,
                    head: '抵扣金额',
                    sortable: true
                },
                AchievementData: {
                    width: 120,
                    head: '业绩日期',
                    sortable: true,
                    renderer: function(val, field, row) {
                        return row.AchievementYear + '.' + row.AchievementMonth;
                    }
                }
            }
        });
    },
    initEvent: function() {
        var _this = this;
        $('#btn_del').click(function() {
            _this.delList();
        });
        $('#btn_add').click(function() {
            _this.addSetting();
        });
        $('#btn_edit').click(function() {
            _this.editSetting();
        });
    },
    editSetting: function() {
        var _this = this;
        if (!_this.currentId) {
            alert('请先选择一条记录！');
            return;
        }
        if (this.editscale == undefined) {
            this.editscale = new Chenxin.UI.EditScaleForm({
                scope: this,
                afterGetFormJson: function(json) {
                    this.editscale.additionalParam.AchievementDate = json.AchievementDate;
                    if (!json.AchievementsMin)
                        json.AchievementsMin = 0;
                    if (!json.AchievementsMax)
                        json.AchievementsMax = 9999999999;
                    if (!json.Deduct)
                        json.Deduct = 0;
                    delete json.AchievementDate;
                }
            });
        }
        if (this.editscale.show()) {
            Chenxin.Fn.getModel(_this.currentId, _this.actionUrl, function(model) {
                model.AchievementDate = model.AchievementYear + '-' + model.AchievementMonth;
                this.fitData(model);
                this.scope.currentModel = model;
            }, this.editscale);
        }
    },
    addSetting: function() {
        if (this.addscale == undefined) {
            this.addscale = new Chenxin.UI.AddScaleForm({
                scope: this,
                afterGetFormJson: function(json) {
                    this.addscale.additionalParam.stime = json.stime;
                    this.addscale.additionalParam.etime = json.etime;
                    if (json.AchievementsMin)
                        this.addscale.additionalParam.min = json.AchievementsMin;
                    else
                        this.addscale.additionalParam.min = 0;
                    if (json.AchievementsMax)
                        this.addscale.additionalParam.max = json.AchievementsMax;
                    else
                        this.addscale.additionalParam.max = 9999999999;
                    this.addscale.additionalParam.Ratio = json.Ratio;
                    if (json.Deduct)
                        this.addscale.additionalParam.Deduct = json.Deduct;
                    else
                        this.addscale.additionalParam.Deduct = 0;
                }
            });
        }
        this.addscale.show()
    },
    initComponent: function() {
        Chenxin.UI.CommissionScale.superclass.initComponent.call(this);
    }
});

/**
*查询面板
*/
Chenxin.UI.CommissionScaleSearch = Chenxin.extend(Chenxin.UI.SearchCommon, {
    id: 'CommissionScale_search',
    initEvent: function() {
        var _this = this;

        //查询按钮
        $('#btnSreach').click(function() {
            _this.scope.reloadGrid("",0);
        });
        
        var date = new Date();
        $("#txtaddtime").focus(function() {
            WdatePicker({
                dateFmt: 'yyyy-MM',
                oncleared: function() {
                    $(this).blur();
                },
                onpicked: function() {
                    $(this).blur();
                }
            });
        }).val(date.getFullYear() + '-' + (date.getMonth() + 1));

        $("#txtendtime").focus(function() {
            WdatePicker({
                dateFmt: 'yyyy-MM',
                oncleared: function() {
                    $(this).blur();
                },
                onpicked: function() {
                    $(this).blur();
                }
            });
        }).val(date.getFullYear() + '-' + (date.getMonth() + 1));
    },
    createComboList: function() {
        //五级联Combo
        var combl = new Chenxin.UI.StructComboList();
        var structs = combl.comboList;

        return structs;
    },
    initComponent: function() {
        Chenxin.UI.CommissionScaleSearch.superclass.initComponent.call(this);
    }
});

$(document).ready(function() {
    this.CommissionScale = new Chenxin.UI.CommissionScale();
});